package com.dongdongshop.config;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect     //表示当前是一个切面类
public class LogAop {

    private Logger logger = LoggerFactory.getLogger(LogAop.class);

    @Pointcut("execution(* com.dongdongshop.service.*.*(..)) || @annotation(com.dongdongshop.log.DongdongLog)")
    public void pointcut(){
    }

    //定义前置,后置,返回,异常,环绕
    @Before("pointcut()")
    public void aopBefore(JoinPoint joinPoint){
        //前置通知打印参数
        Object[] args = joinPoint.getArgs();
        logger.info("正在调用 {} 方法,参数为 {}",joinPoint.getSignature(),args);
    }

    @AfterReturning(value = "pointcut()",returning = "object")
    public void aopAfter(Object object) throws Throwable{
        logger.info("方法调用成功.返回的结果为: {}", JSONObject.toJSONString(object));
    }
}
